package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.entity.StoreEntity;
import com.zw.yzk.kabu.data.network.task.CollectStoreTask;
import com.zw.yzk.kabu.data.network.task.StoreTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.StoreMapper;
import com.zw.yzk.kabu.view.view.StoreView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/14.
 */

public class StorePresenter implements Presenter {

    private UseCase getStoreInfo;
    private UseCase collectStore;
    private AppCompatActivity activity;
    private StoreView storeView;
    private StoreMapper mapper;

    @Inject
    public StorePresenter(@Named("storeUseCase") UseCase getStoreInfo,
                          @Named("collectStoreUseCase") UseCase collectStore,
                          StoreMapper mapper,
                          AppCompatActivity activity) {
        this.getStoreInfo = getStoreInfo;
        this.collectStore = collectStore;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(StoreView view) {
        this.storeView = view;
    }

    public void getStoreInfo(int ownerId, int start) {
        getStoreInfo.execute(new ShopSubscriber(activity), new StoreTask(ownerId, start));
    }

    public void collectStore(int shopId, boolean collect) {
        collectStore.execute(new CollectStoreSubscriber(activity), new CollectStoreTask(shopId, collect ? 1 : 0));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getStoreInfo.unSubscribe();
        collectStore.unSubscribe();
    }

    private class ShopSubscriber extends BaseSubscriber<StoreEntity> {

        public ShopSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(StoreEntity entity) {
            super.onNext(entity);
            storeView.getStoreInfoSuccess(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            storeView.getStoreInfoFailed();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_store_failed);
        }
    }

    private class CollectStoreSubscriber extends BaseSubscriber<Entity> {

        public CollectStoreSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            ToastManager.getInstance().showToast(activity,
                    activity.getResources().getString(storeView.getCollectStatus() ? R.string.collect_success : R.string.cancel_success ));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            storeView.collectFiled();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(storeView.getCollectStatus() ? R.string.collect_failed : R.string.cancel_failed);
        }
    }
}
